package io.gitee.yaoshanli.engine.service;

import io.gitee.yaoshanli.engine.db.JdbcHelper;
import io.gitee.yaoshanli.engine.db.JwDbHelper;
import io.gitee.yaoshanli.engine.db.dialect.Dialect;
import io.gitee.yaoshanli.engine.domain.Page;
import io.gitee.yaoshanli.engine.domain.R;

/**
 * @author 姚善利
 * @classname org.jwflow.core
 * @description 基础服务里
 * @date 2022/9/20 15:02
 */
public class BaseService<T> {

    /**
     *  分页程序
     * @param clazz
     * @param sql
     * @param pageNum
     * @param pageSize
     * @return
     */
    public Page<T> page(Class<T> clazz,String sql,int pageNum,int pageSize)
    {
        Page<T> page=new Page<T>();
        page.setPageNo(pageNum);
        page.setPageSize(pageSize);
        page.setTotalCount(JwDbHelper.queryList(clazz,sql).size());

        String pageSql=sql;
        try {
            //根据链接的驱动判断使用哪种数据库方言
            Dialect dialect= JdbcHelper.getDialect(JwDbHelper.getConnection());
            pageSql = dialect.getPageSql(sql,page);
        } catch (Exception e) {
            e.printStackTrace();
        }
        page.setResult(JwDbHelper.queryList(clazz,pageSql));
        return page;
    }

    public R success()
    {
        return R.ok("成功！");
    }

    public R error()
    {
        return R.fail("失败！");
    }

    public R success(String msg)
    {
        return R.ok(msg);
    }

    public R error(String msg)
    {
        return R.fail(msg);
    }

    public R success(T data)
    {
        return R.ok(data);
    }

    public R error(T data)
    {
        return R.fail(data);
    }
}
